<?php
/*
 RSS2.0.INC.PHP by martin $ 2009/03/08 02:31:51
*/

function createRSS20($time){
 global $mb, $DIVISION;
 $blog_name = BLOG_NAME;
 $copyright = COPY_RIGHT;
 $c_title = $blog_name;
 $root_path = ROOT_PATH; $_self = 'index.php';
 $description = $blog_name.': Recent Entries';
 $rdf_li = ''; $apres = MODE_STATIC ? '/' : '';
 $mod_date = date('D, d M Y H:i:s +0900', $time);
 $generator = 'ppBlog ver.'.PPBLOG_VERSION;
 $items = '';
 $LINES = array();
 $LOGS = my_glob(EXT, 'log/');
 $length = count($LOGS);
 if($length > 0){
  for($j=0;$j<$length;$j++){
   $LINES = array_merge($LINES, my_file($LOGS[$j], FALSE));
   if(count($LINES) > RECENT_ENTRIES){
    $LINES = array_slice($LINES, 0, RECENT_ENTRIES);
    break;
   }
  }
 }

 foreach ($LINES as $line){
  list($uid,$category,$title,$body,) = explode("|", $line);
  if(strpos($uid, 'd')) continue;
  $pubdate = date('D, d M Y H:i:s +0900', $uid);
  if(strpos($title, ',')){
   list($title, $pm_title) = explode(',', $title);
   $pm_link = date('Ym-', $uid).$pm_title.EXT_STATIC;
  } else $pm_link = MODE_STATIC ? $uid.EXT_STATIC : $_self.'?UID='.$uid;
  $title = htmlspecialchars(strip_tags($title));
  if(strstr($category, ',')){
   list($cat1, $cat2) = explode(',', $category);
   $cat = trans_cat($cat1).'/'.trans_cat($cat2).$apres;
  } else $cat = trans_cat($category).$apres;
  $category_tag = $root_path.(MODE_STATIC ? 'category/' : 'index.php?mode=category&amp;aim=').$cat;
  $link = $root_path.$pm_link;
  $body = preg_replace('{\[(?:style|script|escape)\](.+?)\[/(?:style|script|escape)\]}', '', $body);
  $_body = htmlspecialchars(strip_tags($body));
  $content_encoded = str_replace(array('&amp;', '&'), '$', $body);
  $body = preg_replace('{\[g\]([^\/]+?)\[/g\]}', "\\1", $_body);
  $body = preg_replace('{\[file:([^/]+)/\]|\[media:.+?/\]|`}', '', $body);
  $body = htmlspecialchars(my_substr($body, 0, 120));
  $content_encoded = my_parser($content_encoded);
  $content_encoded = preg_replace('{\[file:(.+?):(.+?)/\]}',
  '<a href="'.$root_path.'index.php?mode=download&amp;aim=\\1" title="ダウンロード: \\2 KB">[ファイル: \\1]</a>', $content_encoded);
  $content_encoded = str_replace(array('<>', ' < ',' > ', '&mdash;', '$'),
   array('&gt;&lt;', ' &gt; ', ' &lt; ', '&#x8212;', '&amp;'), $content_encoded);
  $items .= <<<__RSS

<item>
 <title>$title  :: $category</title>
 <link>$link</link>
 <description>$body</description>
 <comments>{$root_path}index.php?mode=comment&amp;TID={$uid}</comments>
 <pubDate>$pubdate</pubDate>
 <category domain="$category_tag">$category</category>
 <guid>{$root_path}index.php?UID=$uid</guid>
 <content:encoded>
 <![CDATA[$content_encoded]]>
 </content:encoded>
</item>
__RSS;
 }
 $rss=<<<__RSS
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
 <channel>
  <title>$blog_name</title>
  <link>$root_path</link>
  <description>$description</description>
  <language>ja-jp</language>
  <lastBuildDate>$mod_date</lastBuildDate>
  <generator>$generator</generator>
  <copyright>$copyright</copyright>
  <category domain="{$root_path}feeds/rss2.0.xml">rssUpdates</category>
  <docs>{$root_path}rss</docs>
 <image>
  <url>{$root_path}Images/logo.png</url>
  <title>$blog_name</title>
  <link>{$root_path}</link>
  </image>
$items
 </channel>
</rss>
__RSS;
 if(preg_match_all('{<img src="([^"]+?)" alt="([^"]*?)"(?:.*?)/>}i', $rss, $mt)){ // icluding image-tags
  $imgs = array();
  $length = count($mt[0]);
  for($i=0; $i < $length; $i++){
   list(,$w,$h,$tw,$th,$alt,,,,$ttl,) = explode(':', $mt[2][$i]);
   $ttl = ($ttl != '') ? ' title="'.$ttl.'"' : '';
   if($tw * $th > 0){
    $imgs[$i] = '<img src="'.$root_path.str_replace('PIX/', 'PIX/s2/', $mt[1][$i]).'" alt="'.$alt.'" width="'.$tw.'" height="'.$th.'"'.$ttl.' />';
   } else {
    $imgs[$i] = '<img src="'.$root_path.$mt[1][$i].'" alt="'.$alt.'" width="'.$w.'" height="'.$h.'"'.$ttl.' />';
   }
  }
  $rss = str_replace($mt[0], $imgs, $rss);
 }
 rewrite('feeds/rss2.0.xml', $rss); unset($LOGS, $LINES);
 /*$rss_sjis = 'rss-sjis.xml';
 rewrite('feeds/'.$rss_sjis, my_convert_encoding(
  str_replace(array('UTF-8', ENCODE, 'rss2.0.xml'), array('SJIS', 'SJIS', $rss_sjis), $rss), 'SJIS', ENCODE)
 );*/
}
?>